/******************************************************************************
 *  _____     _____
 *  \  __\    \  __\
 *   \ \_\\    \ \_\\
 *    \  __\SS  \  __\roject
 *     \_\       \_\
 *
 * Mateusz Maciąg, Rafał Urbański
 * Komputerowe Systemy Sterowania
 *****************************************************************************/

#include "CreateARXDialog.h"
#include "ui_CreateARXDialog.h"

#include <string>

#include "ARXPreset.h"

using namespace std;
using namespace RafMat::UI;

CreateARXDialog::CreateARXDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::CreateARXDialog)
{
    ui->setupUi(this);
    updatePresets();
    refreshConstraints(false);
}

CreateARXDialog::~CreateARXDialog()
{
    delete ui;
}

void CreateARXDialog::updatePresets()
{
    const vector<ARXPresetSPtr> &presets = ARXPreset::presets();
    vector<ARXPresetSPtr>::const_iterator it;

    for(it = presets.begin(); it != presets.end(); ++it)
    {
        ui->presetBox->addItem(QString::fromStdString((*it)->getPresetName()),
                               QVariant::fromValue(*it));
    }
}

ARXPresetSPtr CreateARXDialog::getPreset() const
{
    QVariant data = ui->presetBox->itemData(ui->presetBox->currentIndex());
    return data.value<ARXPresetSPtr>();
}

bool CreateARXDialog::isPresetSelected() const
{
    return ui->usePreset->isChecked();
}

unsigned int CreateARXDialog::getADegree() const
{
    return ui->sbADegree->value();
}

unsigned int CreateARXDialog::getBDegree() const
{
    return ui->sbBDegree->value();
}

unsigned int CreateARXDialog::getCDegree() const
{
    return ui->sbCDegree->value();
}

unsigned int CreateARXDialog::getAgDegree() const
{
    return ui->sbAgDegree->value();
}

unsigned int CreateARXDialog::getD() const
{
    return 1000000;
}

unsigned int CreateARXDialog::getDelay() const
{
    return ui->sbDelay->value();
}

QString CreateARXDialog::getARXName() const
{
    return ui->sbName->text();
}

void CreateARXDialog::refreshConstraints(bool checked)
{
    if(checked)
    {
        ARXPresetSPtr currentPreset = getPreset();
        // Nałóż ograniczenia związane z presetem
        ui->sbADegree->setMinimum(currentPreset->getMinADegree());
        ui->sbBDegree->setMinimum(currentPreset->getMinBDegree());
        ui->sbCDegree->setMinimum(currentPreset->getMinCDegree());
        ui->sbAgDegree->setMinimum(currentPreset->getMinAgDegree());
        ui->sbDelay->setMinimum(currentPreset->getMinDelay());
        ui->sbName->setText(QString::fromStdString(currentPreset->getObjectName()));
    }
    else
    {
        // Zrzuć ograniczenia na parametry:
        ui->sbADegree->setMinimum(0);
        ui->sbBDegree->setMinimum(0);
        ui->sbCDegree->setMinimum(0);
        ui->sbAgDegree->setMinimum(0);
        ui->sbDelay->setMinimum(0);
    }
}

void CreateARXDialog::on_usePreset_toggled(bool checked)
{
    refreshConstraints(checked);
}

void CreateARXDialog::on_presetBox_currentIndexChanged(int index)
{
    index = index;
    refreshConstraints(ui->usePreset->isChecked());
}

